package com.cskaoyan.user.controller;

import com.cskaoyan.mall.commons.exception.ValidateException;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.form.KaptchaCodeRequest;
import com.cskaoyan.user.form.UserRegisterRequest;
import com.cskaoyan.user.form.UserVerifyRequest;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.IUserRegisterService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author 2731235517@qq.com 谢畅
 * @since 2023/01/09 21:32
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IKaptchaService iKaptchaService;

    @Autowired
    private IUserRegisterService iUserRegisterService;

    @PostMapping("/register")
    public ResponseData register(@RequestBody Map map, HttpServletRequest request) {
        UserRegisterRequest userRegisterRequest = new UserRegisterRequest();
        userRegisterRequest.setEmail((String) map.get("email"));
        userRegisterRequest.setUserName((String) map.get("userName"));
        userRegisterRequest.setUserPwd((String) map.get("userPwd"));
        String captcha = (String) map.get("captcha");
        userRegisterRequest.setCaptcha(captcha);

        // 校验 验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(captcha);
        try {
            iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseUtil<>().setErrorMsg(UserRetCode.KAPTCHA_CODE_ERROR.getMessage());
        }

        // 判空
        if (StringUtils.isEmpty(userRegisterRequest.getUserName()) || StringUtils.isEmpty(userRegisterRequest.getUserPwd())
                || StringUtils.isEmpty(userRegisterRequest.getEmail())) {
            return new ResponseUtil<>().setErrorMsg(UserRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }

        // 新建用户
        return iUserRegisterService.register(userRegisterRequest);
    }

    @GetMapping("verify")
    public ResponseData verify(String username,String uuid) {

        //校验参数是否为空
        if (StringUtils.isEmpty(username)||StringUtils.isEmpty(uuid)) {
            throw new ValidateException(UserRetCode.VERIFY_INFO_NOT_EXIST.getCode(),UserRetCode.VERIFY_INFO_NOT_EXIST.getMessage());
        }
        // 校验信息

        UserVerifyRequest userVerifyRequest = new UserVerifyRequest(username,uuid);
        iUserRegisterService.verify(userVerifyRequest);

        return new ResponseUtil<>().setData(null);
    }
}
